﻿///<reference path="events.js" />

(function() {

		var Request = Sys.Net.WebRequest;

	
		var Ajax = {
			post: function(url, params) {
				params["method"] = "POST";
				this.ajax(url, params);
			},

			get: function(url, params) {
				params["method"] = "GET";
				this.ajax(url, params);
			},

			ajax: function(url, params) {
				var request = new Request();
				var method = params["method"] ? params["method"] : "POST";

				var parameters = "";
				for (var property in parameters)
					parameters += "&" + encodeURIComponent(property) + "=" + encodeURIComponent(parameters[property]);

				amp.extend(request.get_headers(), params["headers"] || {});

				request.set_httpVerb(method);
				request.set_url(url);
				if (parameters.length > 0)
					request.set_body(parameters.substring(1));

				var complete = params["onComplete"] || amp.empty;

				request.add_completed(function(executor) {
					var args = {
						"aborted": executor.get_aborted(),
						"responseXml": executor.get_responseAvailable() ? executor.get_xml() : null,
						"responseText": executor.get_responseAvailable() ? executor.get_responseData() : null,
						"statusCode": executor.get_statusCode(),
						"statusText": executor.get_statusText(),
						"getHeader": executor.getResponseHeader,
						"headers": executor.getAllResponseHeaders()
					};
					complete.call(args);
				});
			}
		}



		amp.extend(amp, Ajax);


	})() 